1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 import java.io.File;
36 import java.util.Set;
37 import javax.management.Attribute;
38 import javax.management.MBeanServer;
39 import javax.management.MBeanServerFactory;
40 import javax.management.ObjectInstance;
41 import javax.management.ObjectName;
42 import javax.management.ReflectionException;
43
44 public class LibraryLoaderTest {
45
46 private static final String mletInfo[][] = {
47 {"testDomain:type=MLet,index=0", "UseNativeLib0.html"},
48 {"testDomain:type=MLet,index=1", "UseNativeLib1.html"}
49 };
50
51 public static void main (String args[]) {
52
53 String osName = System.getProperty("os.name");
54 System.out.println("os.name=" + osName);
55 String osArch = System.getProperty("os.arch");
56 System.out.println("os.name=" + osArch);
57
58
59
60
61
62 if ((!(osName.equals("SunOS") && osArch.equals("sparc"))) &&
63 (!(osName.startsWith("Windows") && osArch.equals("x86")))) {
64 System.out.println(
65 "This test runs only on Solaris/SPARC and Windows/x86 platforms");
66 System.out.println("Bye! Bye!");
67 return;
68 }
69
70 String libPath = System.getProperty("java.library.path");
71 System.out.println("java.library.path=" + libPath);
72 String testSrc = System.getProperty("test.src");
73 System.out.println("test.src=" + testSrc);
74 String workingDir = System.getProperty("user.dir");
75 System.out.println("user.dir=" + workingDir);
76
77 String urlCodebase;
78 if (testSrc.startsWith("/")) {
79 urlCodebase =
80 "file:" + testSrc.replace(File.separatorChar, '/') + "/";
81 } else {
82 urlCodebase =
83 "file:/" + testSrc.replace(File.separatorChar, '/') + "/";
84 }
85
86 try {
87
88
89 MBeanServer server = MBeanServerFactory.newMBeanServer();
90
91
92
93 for (int i = 0; i < mletInfo.length; i++) {
94
95
96 ObjectName mlet = new ObjectName(mletInfo[i][0]);
97 server.createMBean("javax.management.loading.MLet", mlet);
98 System.out.println("MLet = " + mlet);
99
100
101
102 String libraryDirectory =
103 (String) server.getAttribute(mlet, "LibraryDirectory");
104 System.out.println("Old Library Directory = " +
105 libraryDirectory);
106 Attribute attribute =
107 new Attribute("LibraryDirectory", workingDir);
108 server.setAttribute(mlet, attribute);
109 libraryDirectory =
110 (String) server.getAttribute(mlet, "LibraryDirectory");
111 System.out.println("New Library Directory = " +
112 libraryDirectory);
113
114
115
116 String mletURL = urlCodebase + mletInfo[i][1];
117 System.out.println("MLet URL = " + mletURL);
118 Object[] params = new Object[] { mletURL };
119 String[] signature = new String[] {"java.lang.String"};
120 Object res[] = ((Set) server.invoke(mlet,
121 "getMBeansFromURL",
122 params,
123 signature)).toArray();
124
125
126
127 for (int j = 0; j < res.length; j++) {
128
129
130 if (res[j] instanceof Throwable) {
131 ((Throwable) res[j]).printStackTrace(System.out);
132 System.out.println("Failed to load the MBean #" + j +
133 ". The shown Throwable was caught.");
134 System.exit(1);
135 }
136
137
138
139
140 Object result = null;
141 try {
142 ObjectName mbean =
143 ((ObjectInstance) res[j]).getObjectName();
144 result = server.getAttribute(mbean, "Random");
145 System.out.println("MBean #" + j + " = " + mbean);
146 System.out.println("Random number = " + result);
147 } catch (ReflectionException e) {
148 e.getTargetException().printStackTrace(System.out);
149 System.out.println("A ReflectionException, wrapping " +
150 "the shown exception, occured when" +
151 " attempting to invoke a native " +
152 "library based operation.");
153 System.exit(1);
154 }
155 }
156 }
157 } catch (Exception e) {
158 e.printStackTrace(System.out);
159 System.out.println(e.getMessage());
160 System.out.println("Unexpected error");
161 System.exit(1);
162 }
163 System.out.println("Bye! Bye!");
164 }
165 }